function getUrlSearchObj(){
    var obj = {};
    var search = window.location.search.substr(1);
    var list = search.split("&");
    list.forEach(function(e){
        var item = unescape(e);
        var kv = item.split("=");
        if(kv.length==2){
            obj[kv[0]] = kv[1];
        }
    });
    return obj;
}
$(document).ready(function(){
    $("textarea").css("background-color","#CCCCCC");
    var infoHtml = $("#info").html();
    if(infoHtml[0] == "}"){
        infoHtml = Base64.atob(infoHtml.substr(1));
        infoHtml = pako.inflate(infoHtml, {to:"string"});
    }

    // set fold toggle
    infoHtml = infoHtml.replace(/{[\|]*/g, "{<input type=\"button\" value=\"...\"></input><span>\n")
    infoHtml = infoHtml.replace(/},[\|]*/g, "</span>},\n");
    infoHtml = infoHtml.replace(/\?/g, "&nbsp;");
    infoHtml = infoHtml.replace(/[\n\|]/g, "<br/>");


    var pathname = window.location.pathname;
    if(pathname.match("/get$") ){
        var strFormat = "[\"<a class=\"pushstr\" data-str=\"$1\" href=\"#\">$1</a>\"]"
        infoHtml = infoHtml.replace(/\[\"([^\"]*)\"\]/g, strFormat);

        var numFormat = "[<a class=\"pushnum\" data-num=\"$1\" href=\"#\">$1</a>]"
        infoHtml = infoHtml.replace(/\[(\d+(\.\d+)?)\]/g, numFormat);
        var argv = JSON.parse(getUrlSearchObj().argv || "[]");
        console.log(getUrlSearchObj());
        $("#info").html(infoHtml);

        $("a.pushstr").click(function(e){
            var arg = $(e.target).attr("data-str")
            argv.push(arg);
            window.location.search="?argv="+JSON.stringify(argv);
        });
        $("a.pushnum").click(function(e){
            var arg = Number($(e.target).attr("data-num"));
            argv.push(arg);
            window.location.search="?argv="+JSON.stringify(argv);
        });
    }else{
        var pathname = window.location.pathname;
        var aFormat = "[\"<a href=\""+window.location.pathname+"/$1\">$1</a>\"]"
        infoHtml = infoHtml.replace(/\[\"([^\"\[\]]*)\"\]/g, aFormat);
        $("#info").html(infoHtml);
    }


    $("#rawToggle").click(function(){
        var pathname = window.location.pathname;
        if(pathname.match("\/get$")){
            window.location.pathname = pathname.replace("\/get$","/rawget");
        }else if(pathname.match("\/rawget$")){
            window.location.pathname = pathname.replace("\/rawget$","/get");
        }else{
            window.location.pathname = pathname+"/get"
        }
    });
})
